
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import GridSearchCV
import warnings
warnings.filterwarnings('ignore')


# 1、读取数据：从乳腺癌原始数据.xlsx文件中读取数据；检查基本信息
df = pd.read_excel('乳腺癌原始数据.xlsx')
print(df.head(10))
print(df.info())


# 2、删除包含任何缺失值的行（3分）
df.dropna(inplace=True)
print(df.head(10))

# 3、提取数值特征，提取目标变量（3分）
# BIRADS等级, 年龄, 肿块形状, 肿块边缘, 肿块密度, 形状密度比
X = df.drop(['严重度'],axis=1)
y = df['严重度']

# 4、划分训练集和测试集（3分）
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.7, random_state=42)

# 5、选择逻辑回归，决策树，GBDT三个模型进行建模。（3分）

clf = LogisticRegression(random_state=0,intercept_scaling=1,max_iter=100)




clf2 = DecisionTreeClassifier(random_state=0,min_samples_split=2,min_samples_leaf=1)




clf3 = GradientBoostingClassifier(random_state=0,min_samples_split=2,min_samples_leaf=1)




# 6、使用GridSearchCV进行超参数调优，要求最少优化模型至少三个参数（5分）

clf6 = GridSearchCV(clf,{'random_state':[1,2,3],'intercept_scaling':[2,3,4],'max_iter':[5,6,7]},cv=5,scoring='accuracy')
lrw = clf6.fit(X_train,y_train)
print(clf6.best_estimator_)
print(clf6.best_score_)


clf61 = GridSearchCV(clf2,{'random_state':[1,2,3],'min_samples_split':[2,3,4],'min_samples_leaf':[5,6,7]},cv=5,scoring='accuracy')
clf61.fit(X_train,y_train)
print(clf61.best_estimator_)
print(clf61.best_score_)

clf62 = GridSearchCV(clf3,{'random_state':[1,2,3],'min_samples_split':[2,3,4],'min_samples_leaf':[5,6,7]},cv=5,scoring='accuracy')
clf62.fit(X_train,y_train)
print(clf62.best_estimator_)
print(clf62.best_score_)


# 7、选择合适的模型评估。（5分）
if clf6.best_score_ > clf61.best_score_ and clf6.best_score_ > clf62.best_score_:
    print(f"逻辑回归{clf6.best_score_}")
elif clf61.best_score_ > clf6.best_score_ and clf61.best_score_ > clf62.best_score_:
    print(f"决策树{clf61.best_score_}")
elif clf62.best_score_ > clf6.best_score_ and clf62.best_score_ > clf61.best_score_:
    print(f"GBDT{clf61.best_score_}")